package com.zh.api.controller.family;


import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.familyInfo.domain.IllFamilyHistoryDO;
import com.zh.familyInfo.service.IllFamilyHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@Api("老人疾病信息【家族病史控制层】")
public class IllFamilyHistoryController extends BaseController {
	@Autowired
	private IllFamilyHistoryService illFamilyHistoryService;

	@Log("显示当前用户所有家族病史信息")
	@ResponseBody
	@RequestMapping(value = "listIllFamilyHistory",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO listIllFamilyHistory(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		params.put("familyNumber",getFamilyUser().getNumber());
		//查询列表数据
        Query query = new Query(params);
		List<IllFamilyHistoryDO> illFamilyHistoryList = illFamilyHistoryService.list(query);
		int total = illFamilyHistoryService.count(query);
		PageUtils pageUtils = new PageUtils(illFamilyHistoryList, total);
		return  ResultVO.success(pageUtils);
	}


	@Log("编辑家族个人病史信息")
	@ResponseBody
	@RequestMapping(value = "editIllFamilyHistoryById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public ResultVO editIllFamilyHistoryById(@RequestBody IllFamilyHistoryDO illFamilyHistory){
		if (illFamilyHistory==null|| CheckEmptyUtil.isEmpty(illFamilyHistory.getId())) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		IllFamilyHistoryDO illFamilyHistorys = illFamilyHistoryService.get(illFamilyHistory.getId());
	    return ResultVO.success(illFamilyHistorys);
	}

	@Log("新增家族病史信息")
	@ResponseBody
	@RequestMapping(value = "saveIllFamilyHistory",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO saveIllFamilyHistory(@RequestBody IllFamilyHistoryDO illFamilyHistory){
		if (illFamilyHistory==null) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		illFamilyHistory.setFamilyNumber(getFamilyUser().getNumber());
		if(illFamilyHistoryService.save(illFamilyHistory)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	@Log("更新家族病史信息")
	@ResponseBody
	@RequestMapping(value = "updateIllFamilyHistory",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO updateIllFamilyHistory(@RequestBody IllFamilyHistoryDO illFamilyHistory){
		if (illFamilyHistory==null|| CheckEmptyUtil.isEmpty(illFamilyHistory.getId())) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		illFamilyHistoryService.update(illFamilyHistory);
		return ResultVO.success();
	}

	@Log("删除家族病史信息")
	@ResponseBody
	@RequestMapping(value = "removellFamilyHistoryById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO removellFamilyHistoryById(@RequestBody IllFamilyHistoryDO illFamilyHistory){
		if (illFamilyHistory==null|| CheckEmptyUtil.isEmpty(illFamilyHistory.getId())) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(illFamilyHistoryService.remove(illFamilyHistory.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
@RequestMapping(value = "batchRemoveFamilyHistoryByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
@ApiOperation("批量删除家族病史信息")
public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
	if(jsonParam==null){
		return ResultVO.error(ResultEnum.PARAME_ERROR);
	}
	List<String> ids= (List<String>)jsonParam.get("ids");
	if(CheckEmptyUtil.isEmpty(ids)){
		return ResultVO.error(ResultEnum.PARAME_ERROR);
	}
	String[] strs1=ids.toArray(new String[ids.size()]);
	if(illFamilyHistoryService.batchRemove(strs1)>0){
		return ResultVO.success();
	}
	return ResultVO.error();
}
}
